﻿using UnityEngine;
using System.Collections;
using System;
using LitJson;

public class TimeInterval
{
	public DateTime begin = DateTime.MinValue;
	public DateTime end = DateTime.MaxValue;
}

public static class TimeUtils
{
    static public int currentEpochTime
    {
        get
        {
            return ConvertDateTimeToEpochTime(DateTime.UtcNow);
        }
    }

    static DateTime epochTimeBegin = new DateTime(1970, 1, 1);
    static public DateTime ConvertEpochTimeToDateTime(long epochTime)
    {
        DateTime dateTime = epochTimeBegin;
        return dateTime.AddSeconds(Convert.ToDouble(epochTime));
    }

    static public int ConvertDateTimeToEpochTime(DateTime dateTime)
    {
        TimeSpan timeSpan = dateTime - epochTimeBegin;
        return (int)timeSpan.TotalSeconds;
    }

    static public bool IsDateTimeInTimeInterval(TimeInterval timeInterval, DateTime dateTime)
    {
        bool beginCheck = false;
        bool endCheck = false;

        if (timeInterval.begin <= dateTime) beginCheck = true;
        if (timeInterval.end >= dateTime) endCheck = true;
        return beginCheck && endCheck;
    }

    static public double getTimestamp()
    {
        return (DateTime.UtcNow - epochTimeBegin).TotalMilliseconds;
    }

    //00:00:00
    static public string getTimeFormat(int secondsBegin, int secondsEnd)
    {
        TimeSpan tsBegin = new TimeSpan(0, 0, secondsBegin);
        TimeSpan timeEnd = new TimeSpan(0, 0, secondsEnd);

        TimeSpan ts = timeEnd.Subtract(tsBegin).Duration();
        string dayStr = ts.Days.ToString();
        string deltaTime = string.Format("{0:00}", ts.Hours + (ts.Days * 24)) + ":" + string.Format("{0:00}", ts.Minutes) + ":" + string.Format("{0:00}", ts.Seconds);
        //		string deltaTime = string.Format("{0:00}:{0:00}:{0:00}",ts.Hours,ts.Minutes,ts.Seconds);
        return deltaTime;
    }

    //00:00
    static public string getTimeFormatMinute(int secondsBegin, int secondsEnd)
    {
        TimeSpan tsBegin = new TimeSpan(0, 0, secondsBegin);
        TimeSpan timeEnd = new TimeSpan(0, 0, secondsEnd);

        TimeSpan ts = timeEnd.Subtract(tsBegin).Duration();
        string dayStr = ts.Days.ToString();
        string deltaTime = string.Format("{0:00}", ts.Minutes) + ":" + string.Format("{0:00}", ts.Seconds);
        //		string deltaTime = string.Format("{0:00}:{0:00}:{0:00}",ts.Hours,ts.Minutes,ts.Seconds);
        return deltaTime;
    }

    #region Server Time
    private static int _lastServerTime;
    private static int _lastLocalTime;

    public static int GetServerTime()
    {
        return _lastServerTime + Math.Max(0, currentEpochTime - _lastLocalTime);
    }

    public static void UpdateServerTime(int serverTime, int lastLocalTime)
    {
        int lastDay = 0;
        if (_lastServerTime != 0)
        {
            lastDay = TimeUtils.ConvertEpochTimeToDateTime(_lastServerTime).Day;
        }
        _lastServerTime = serverTime;
        int curDay = TimeUtils.ConvertEpochTimeToDateTime(_lastServerTime).Day;
        if (lastDay != 0 && curDay != lastDay)
        {
            AppFacade.eventManager.DispatchEvent(EventDef.SYS_ANOTHER_DAY);
        }

        if (lastLocalTime <= 0)
        {
            _lastLocalTime = currentEpochTime;
        }
        else
        {
            _lastLocalTime = lastLocalTime;
        }
    }
    #endregion
}